/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO.InformationFilter;

import DAO.DAOAccess;
import Info.CategoryPageInfo;
import Info.PeriodInfo;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author tran tuan tai
 */
public class CategoryPageDAO
{

    public Double GetProb(PeriodInfo period, Integer categoryID)
    {
        String sql = "SELECT PERCENTCATEGORY FROM CATEGORYPAGE WHERE PERIOD = " + period.GetID() + " AND CATEGORY = " + categoryID ;
        Double result = null;
        try
        {
            ResultSet rs = DAOAccess.ExcuteQuery(sql);
            rs.beforeFirst();

            while (rs.next())
            {
                result = rs.getDouble(1);
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return result;
        
    }

    public void DoEmpty()
    {
        try
        {
            java.sql.Connection conn = DAOAccess.GetConnection();
            String sql = "DELETE FROM CategoryPage";
            PreparedStatement stat = conn.prepareCall(sql);
            stat.execute();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public int Insert(CategoryPageInfo info)
    {
        int id = -1;
        try
        {
            java.sql.Connection conn = DAOAccess.GetConnection();
            String sql = "INSERT INTO CATEGORYPAGE(PERIOD, CATEGORY, NPAGE , PERCENTCATEGORY) VALUES(?, ?, ?, ?)";

            PreparedStatement statement = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            statement.setInt(1, info.period);
            statement.setInt(2, info.category);
            statement.setInt(3, info.totalPage);
            statement.setDouble(4, info.percentCategory);

            statement.executeUpdate();
            ResultSet result = statement.getGeneratedKeys();
            result.first();
            id = result.getInt(1);
            statement.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return id;
    }


}
